# $Id: Makefile,v 1.6 2011/04/18 17:14:00 lukagarb Exp $
#

PYTHON=`which python3`
DESTDIR=/
BUILDIR=$(CURDIR)/debian/python-nipap-build
PROJECT=nipap

all:
#	$(PYTHON) setup.py build
	@echo "make source - Create source package"
	@echo "make install - Install on local system"
	@echo "make buildrpm - Generate a rpm package"
	@echo "make builddeb - Generate a deb package"
	@echo "make bumpversion - Bump version to latest in ../NEWS file"
	@echo "make clean - Get rid of scratch and byte files"

source: sql2py
	$(PYTHON) setup.py sdist

install: sql2py
	$(PYTHON) setup.py install

buildrpm: sql2py
	$(PYTHON) setup.py bdist_rpm --post-install=rpm/postinstall --pre-uninstall=rpm/preuninstall

builddeb: sql2py
	# build the source package in the parent directory
	# then rename it to project_version.orig.tar.gz
	$(PYTHON) setup.py sdist --dist-dir=../ #--prune
	rename -f 's/$(PROJECT)-(\d.*)\.tar\.gz/$(PROJECT)_$$1\.orig\.tar\.gz/' ../*
	# build the package
	debuild -us -uc

distclean:
realclean:
test:
check:

upload:
	$(PYTHON) setup.py sdist upload

clean:
	$(PYTHON) setup.py clean
	rm -rf .pybuild/ MANIFEST dist/ debian/tmp debian/nipap-common \
		debian/$(PROJECT) debian/nipapd debian/files .pc/ nipapd.8* \
		nipap-passwd.1* debian/nipapd.debhelper.log \
		debian/nipapd.postinst.debhelper debian/nipapd.postrm.debhelper \
		debian/nipapd.prerm.debhelper debian/nipapd.substvars \
		debian/nipap-common.debhelper.log \
		debian/nipap-common.postinst.debhelper \
		debian/nipap-common.prerm.debhelper debian/nipap-common.substvars \
		debian/.debhelper debian/debhelper-build-stamp nipap.egg-info/
	find . -name '*.pyc' -delete

VER := $(shell head -n1 ../NEWS | awk '{print $$2}')
DB_VER := $(shell grep "COMMENT ON DATABASE %s IS" sql/ip_net.plsql | sed 's/.*version: \([0-9]\+\).*/\1/')
bumpversion:
	# update debian package nipapd's config and postinst script with current db
	# version
	sed -i 's/^CURRENT_DB_VERSION.*/CURRENT_DB_VERSION=$(DB_VER)/' debian/nipapd.config debian/nipapd.postinst
	# replace version & db_version number in nipap/__init__.py
	sed -i 's/\(__version__\s*= \)"[^"]\+"/\1"$(VER)"/' nipap/__init__.py
	sed -i "s/\(__db_version__\s*= \)[0-9]\+/\1$(DB_VER)/" nipap/__init__.py
	# update debian/changelog
	../utilities/news2dch.py ../NEWS debian/changelog

sql2py:
	echo '""" This file contains the nipap postgres db schemas.' > nipap/db_schema.py
	echo '    DO NOT EDIT THIS FILE. It is generated from sql/*.plsql files.' >> nipap/db_schema.py
	echo "    Use 'make sql2py' to update" >> nipap/db_schema.py
	echo '"""' >> nipap/db_schema.py
	echo '' >> nipap/db_schema.py
	echo 'functions = """' >> nipap/db_schema.py
	cat sql/functions.plsql >> nipap/db_schema.py
	echo '"""' >> nipap/db_schema.py
	echo '' >> nipap/db_schema.py
	echo 'ip_net = """' >> nipap/db_schema.py
	cat sql/ip_net.plsql >> nipap/db_schema.py
	echo '"""' >> nipap/db_schema.py
	echo '' >> nipap/db_schema.py
	echo 'triggers = """' >> nipap/db_schema.py
	cat sql/triggers.plsql >> nipap/db_schema.py
	echo '"""' >> nipap/db_schema.py
	echo '' >> nipap/db_schema.py
	echo 'upgrade = [' >> nipap/db_schema.py
	for U in `ls sql/upgrade*.plsql | sort -n`; do \
		echo '"""' >> nipap/db_schema.py; \
		cat $$U >> nipap/db_schema.py; \
		echo '""",' >> nipap/db_schema.py; \
	done
	echo ']' >> nipap/db_schema.py
